#include "JingleExtension.h"
#include "util.h"

namespace cm_nat
{
    JingleExtension::JingleExtension(const Tag* tag ) : StanzaExtension( ExtJingle ),m_reason(NULL),m_jingleTag(NULL)
    {
        if( tag ){
            m_jingleTag = tag->clone();
        }
    }

    JingleExtension::~JingleExtension()
    {
        cm_xmpp::util::clearList(m_contents);
        if( m_reason ){
            delete m_reason;
            m_reason = NULL;
        }
    }


    bool JingleExtension::addContent( Content* p_content )
    {
        if( !p_content )
            return false;

        m_contents.push_back(p_content);
        return true;
    }

    void JingleExtension::setSessionID(const std::string &sid)
    {
        m_sid = sid;
    }

    std::string JingleExtension::getSessionID()
    {
        return m_sid;
    }

    void JingleExtension::setInitiator(const std::string &initiator)
    {
        m_initiator = initiator;
    }

    std::string JingleExtension::getInitiator()
    {
        return m_initiator;
    }


    void JingleExtension::setResponder(const std::string &responder)
    {
        m_responder = responder;
    }

    std::string JingleExtension::getResponder()
    {
        return m_responder;
    }

    void JingleExtension::setAction(const std::string &action)
    {
        m_action = action;
    }

    std::string JingleExtension::getAction()
    {
        return m_action;
    }

    const std::string& JingleExtension::filterString() const
    {
        static const std::string filter = "/iq/jingle[@xmlns='" + XMLNS_JINGLE + "']";

        return filter;
    }

    bool JingleExtension::parse(const Tag* p_tag)
    {
        if( p_tag->name()!= "jingle" )
            return false;

        m_sid = p_tag->findAttribute("sid");
        m_action = p_tag->findAttribute("action");
        m_initiator = p_tag->findAttribute("initiator");
        m_responder = p_tag->findAttribute("responder");

        const TagList& contents = p_tag->findChildren( "content" );
        TagList::const_iterator it = contents.begin();
        for( ; it != contents.end(); ++it ){
            Content* p_cont = new Content;
            p_cont->parse( (*it) );
            addContent(p_cont);
        }
/*
        Tag* reason = p_tag->findChild("reason");
        if( reason ){
            Reason* p_reason = new Reason;
            p_reason->parse( reason );
        }
*/

        return true;
    }

    Tag* JingleExtension::tag() const
    {   
        Tag* p_tag = new Tag( "jingle" );
        p_tag->setXmlns( XMLNS_JINGLE );
        p_tag->addAttribute("sid", m_sid);
        p_tag->addAttribute("action", m_action);

        if( !m_initiator.empty()){
            p_tag->addAttribute("initiator", m_initiator);
        }
        if( !m_responder.empty()){
            p_tag->addAttribute("responder", m_responder);
        }

        ContentList::const_iterator it= m_contents.begin();
        for(; it!=m_contents.end(); ++it ){
            p_tag->addChild( (*it)->tag() );
        }

        return p_tag;
    }
}